from selenium import webdriver
import time
from parse import parse_pagesource
from parse import createDictCSV

class Ranzhi():
      def setUp(self):
         self.driver = webdriver.Firefox() #选择火狐浏览器
      def test_ranzhi(self):
         pass
      def tearDown(self):
        self.driver.quit()#退出浏览器

#driver = webdriver.Chrome(executable_path=r'D:\SomeSetupFile\chromedriver.exe')

driver = webdriver.PhantomJS(executable_path = r'D:\SomeSetupFile\phantomjs-2.1.1-windows\bin\phantomjs.exe',service_args=['--ignore-ssl-errors=true', '--ssl-protocol=TLSv1'])
driver.set_window_size(1366, 30000)
driver.get('http://www.zyqc.cc/Gong_Gao_Cha_Xun/NoticeV2/')
time.sleep(5)
ele = driver.find_element_by_id('CheXin')
ele.send_keys('乘用车')
driver.find_element_by_id('cmdZCQ').click()
for i in range(1,15):
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="p_btns"]//a[contains(text(),"%d")]' % (i*5+1)).click()
'''
for i in range(277, 278):
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="p_btns"]//a[contains(text(),"%d")]' % (i)).click()
'''
for i in range(74,291):
    print(i)
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="p_btns"]//a[contains(text(),"%d")]' % (i)).click()
    time.sleep(2)
    size = len(driver.find_elements_by_xpath('//*[@id="result_body"]/tr'))

    for m in range(0,int(size/2)):
        cur = driver.current_window_handle
        driver.find_element_by_xpath('//*[@id="result_body"]/tr[%d]/td[2]/a' % (2*m+1)).click()

        Handles = driver.window_handles
        for handle in Handles:
            if cur == handle:
                continue
            driver.switch_to.window(handle);
            time.sleep(2)
            result = parse_pagesource(driver.page_source)
            createDictCSV("abc.csv",result)


            driver.close()
        driver.switch_to.window(cur)
driver.quit()




